﻿using BUS;
using DAO;
using OnlineDigitalCameraWebsite.Class;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineDigitalCameraWebsite.Admin
{
    public partial class CameraList_Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDSModel();
                LoadDSSer();
                LoadDSManufaturer();
                txtdes.Language = "vi";
                CKFinder.FileBrowser _FileBrowserNL = new CKFinder.FileBrowser();
                _FileBrowserNL.BasePath = "/ckfinder/";
                _FileBrowserNL.SetupCKEditor(txtdes);
                  

                string type = Request.QueryString["type"].ToString();
                string id = Request.QueryString["id"].ToString();
                if (type == "them")
                {
                    lblTieuDe.Text = "Thêm camera";
                    lblTieuDe.DataBind();
                }
                else
                {
                    lblTieuDe.Text = "Cập Nhật camera";
                    lblTieuDe.DataBind();
                    lblLink.Text = "Xem chi tiết camera";
                    lblLink.NavigateUrl = "/ChiTiet.aspx?cate=camera&id=" + id;
                    lblTieuDe.DataBind();
                    LoadThongTin(id);
                }
            }
        }

        private void LoadDSManufaturer()
        {
            ManufacturerBUS mBUS = new ManufacturerBUS();
            ddlmanufacturerId.DataSource = mBUS.GetAll();
            ddlmanufacturerId.DataValueField = "manufacturerId";
            ddlmanufacturerId.DataTextField = "manufacturerNames";
            ddlmanufacturerId.DataBind();
        }

        private void LoadDSSer()
        {
            SeriesBUS mBUS = new SeriesBUS();
            ddlseriesId.DataSource = mBUS.GetByAll();
            ddlseriesId.DataValueField = "seriesId";
            ddlseriesId.DataTextField = "seriesNames";
            ddlseriesId.DataBind();
        }

        private void LoadDSModel()
        {
            ModelBUS mBUS = new ModelBUS();
            ddlModel.DataSource = mBUS.GetByAll();
            ddlModel.DataValueField = "modelId";
            ddlModel.DataTextField = "modelNames";
            ddlModel.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string type = Request.QueryString["type"].ToString();
                string id = Request.QueryString["id"].ToString();
                CameraBUS cBUS = new CameraBUS();
                if (type == "them")
                {
                    Camera a = new Camera();
                    a.camNames = txtName.Text;
                    string hinhanh = "";
                    if (FileUpload1.HasFile)
                    {
                        string ext = Path.GetExtension(FileUpload1.FileName);
                        string name = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
                        string ten = "/Uploads/images/camera_moi/" + name + "_" + XuLy.GetDateTimeNow() + ext;
                        //if (!Image.Exists)
                        //{
                        FileUpload1.SaveAs(Server.MapPath(ten));
                        hinhanh = ten;
                        //}
                    }
                    else
                    {
                        hinhanh = "/Uploads/images/no_image.png";
                    }
                    a.camImages = hinhanh;
                    a.camPrice = double.Parse(txtprice.Text);
                    a.camDetails = txtdes.Text;
                    a.camMaxResolution = txtcamMaxResolution.Text;
                    a.camSensorType = txtSensorType.Text;
                    a.camSensorSize = txtSensorSize.Text;
                    a.camIso = txtcamIso.Text;
                    a.camFocalLengthMulti = txtFocalLengthMulti.Text;
                    a.camScreenSize = txtcamScreenSize.Text;
                    a.camMinShutterSpeed = txtcamMinShutterSpeed.Text;
                    a.camMaxShutterSpeed = txtcamMaxShutterSpeed.Text;
                    a.modelId = int.Parse(ddlModel.SelectedValue.ToString());
                    a.manufacturerId = int.Parse(ddlmanufacturerId.SelectedValue.ToString());
                    a.seriesId = int.Parse(ddlseriesId.SelectedValue.ToString());
                    a.camStatus = bool.Parse(ddlactive.SelectedValue.ToString());
                    cBUS.Insert(a);
                    Response.Redirect("/Admin/CameraList.aspx");
                }
                else
                {

                    Camera a = new Camera();
                    int idma = int.Parse(id);
                    a.camId = idma;
                    a.camNames = txtName.Text;
                    string hinhanh = "";
                    if (FileUpload1.HasFile)
                    {
                        string ext = Path.GetExtension(FileUpload1.FileName);
                        string name = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
                        string ten = "/Uploads/images/camera_moi/" + name + "_" + XuLy.GetDateTimeNow() + ext;
                        //FileInfo Image = new FileInfo(Server.MapPath(ten));
                        //if (!Image.Exists)
                        //{
                        
                        FileUpload1.SaveAs(Server.MapPath(ten));
                        hinhanh = ten;
                        //}
                    }
                    else
                    {
                        hinhanh = Image1.ImageUrl;
                    }
                    a.camImages = hinhanh;
                    a.camPrice = double.Parse(txtprice.Text);
                    a.camDetails = txtdes.Text;
                    a.camMaxResolution = txtcamMaxResolution.Text;
                    a.camSensorType = txtSensorType.Text;
                    a.camSensorSize = txtSensorSize.Text;
                    a.camIso = txtcamIso.Text;
                    a.camFocalLengthMulti = txtFocalLengthMulti.Text;
                    a.camScreenSize = txtcamScreenSize.Text;
                    a.camMinShutterSpeed = txtcamMinShutterSpeed.Text;
                    a.camMaxShutterSpeed = txtcamMaxShutterSpeed.Text;
                    a.modelId = int.Parse(ddlModel.SelectedValue.ToString());
                    a.manufacturerId = int.Parse(ddlmanufacturerId.SelectedValue.ToString());
                    a.seriesId = int.Parse(ddlseriesId.SelectedValue.ToString());
                    a.camStatus = bool.Parse(ddlactive.SelectedValue.ToString());
                    cBUS.Edit(a); 
                    lbThongBao.Text = "Cập nhật thành công";
                    lbThongBao.Visible = true;
                    LoadThongTin(id);
                }
            }
            catch (Exception ex)
            {
                lbThongBao.Text = "Lỗi: " + ex.Message;
                lbThongBao.Visible = true;
            }
        }

        private void LoadThongTin(string id)
        {
            int idma = int.Parse(id);
            Camera tt = new Camera();
            CameraBUS cBUS = new CameraBUS();
            tt = cBUS.GetById(idma);
            txtName.Text = tt.camNames;
            Image1.ImageUrl = tt.camImages;
            txtprice.Text = tt.camPrice.ToString();
            txtdes.Text = tt.camDetails;
            txtcamMaxResolution.Text = tt.camMaxResolution;
            txtSensorType.Text = tt.camSensorType;
            txtSensorSize.Text = tt.camSensorSize;
            txtcamIso.Text = tt.camIso;
            txtFocalLengthMulti.Text = tt.camFocalLengthMulti;
            txtcamScreenSize.Text = tt.camScreenSize;
            txtcamMinShutterSpeed.Text = tt.camMinShutterSpeed;
            txtcamMaxShutterSpeed.Text = tt.camMaxShutterSpeed;
            ddlModel.SelectedValue = tt.modelId.ToString();
            ddlmanufacturerId.SelectedValue =  tt.manufacturerId.ToString();
            ddlseriesId.SelectedValue = tt.seriesId.ToString();
            ddlactive.SelectedValue = tt.camStatus.ToString();


        }
    }
}